//
//  MessageTableViewCell.swift
//  MT
//
//  Created by ooops on 2019/12/15.
//  Copyright © 2019 张强. All rights reserved.
//

import UIKit
import MTCategory

class MessageTableViewBaseCell: UITableViewCell {
    
    //static let reuseIdentity: String = "MessageTableViewCellID"
    
    open class func reuseIdentity() -> String {
        return "MessageTableViewCellID"
    }
    
    internal var info: JCVerificationInfo!
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = UIColor(hex: 0x1D1A1A)
        self.selectionStyle = .none
        setupUI()
        makeConstraint()
    }
    
    func bindData(_ info: JCVerificationInfo) {
        self.info = info
        avatorImgV.image = UIImage(named: defaultPlaceholderIcon60x60)
        switch info.state {
        case JCVerificationType.wait.rawValue:
            descLabel.text = "请求添加您为好友"
        case JCVerificationType.receive.rawValue:
            descLabel.text = info.resaon
        case JCVerificationType.accept.rawValue:
            descLabel.text = "同意了你的好友请求"
        case JCVerificationType.reject.rawValue:
            descLabel.text = "对方拒绝了你的好友请求"
        default:
            break
        }
        JMSGUser.userInfoArray(withUsernameArray: [info.username]) { (result, error) in
            if error == nil {
                let users = result as! [JMSGUser]
                let user = users.first
//                user?.thumbAvatarData({ (data, id, error) in
//                    if let data = data {
//                        let image = UIImage(data: data)
//                        self.avatorImgV.image = image
//                    }
//                })
                self.avatorImgV.mtSetImage(user?.avatar, nil)
                self.nameLabel.text = user?.displayName()
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var cellModel: MListModel? {
        didSet {
            avatorImgV.setImageWithURLString(cellModel?.avator, placeholder: UIImage(named: "icon_gift_display"))
            nameLabel.text = cellModel?.name
            descLabel.text = cellModel?.desc
        }
    }
    
    func setupUI() {
        contentView.addSubview(avatorImgV)
        contentView.addSubview(nameLabel)
        contentView.addSubview(descLabel)
        contentView.addSubview(lineView)
    }
    
    func makeConstraint() {
        avatorImgV.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(16)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(44)
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatorImgV.snp.right).offset(11)
            make.bottom.equalTo(contentView.snp.centerY).offset(-2)
        }
        
        descLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.top.equalTo(contentView.snp.centerY).offset(2)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    lazy var avatorImgV: UIImageView = {
        let imgV: UIImageView = UIImageView()
        imgV.contentMode = .scaleAspectFill
        imgV.clipsToBounds = true
        imgV.layer.masksToBounds = true
        imgV.layer.cornerRadius = 22
        return imgV
    }()
    
    lazy var nameLabel: UILabel = {
        let lbl: UILabel = UILabel()
        lbl.textColor = UIColor(hex: 0xFFFFFF)
        lbl.font = PingFangSCMediumFont(14)
        lbl.numberOfLines = 1
        lbl.sizeToFit()
        return lbl
    }()
    
    lazy var descLabel: UILabel = {
        let lbl: UILabel = UILabel()
        lbl.textColor = ListViewSubTitleColor
        lbl.font = PingFangSCRegularFont(12)
        lbl.numberOfLines = 1
        lbl.sizeToFit()
        return lbl
    }()
    
    lazy var lineView: UIView = {
        let v: UIView = UIView()
        v.backgroundColor = UIColor.seprateLine.x707070
        return v
    }()

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
